﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Common;
using Controller;

namespace CodeFactory
{
    public partial class ServerConnecter : Form
    {
        private ServerConnectBO _scBO;
        public ServerConnectBO ScBO
        {
            get 
            {
                if (_scBO == null)
                    _scBO = FactoryControllerBO.CurrentFA.CreateServerConnectBOInstance();
                return _scBO; 
            }
        }

        private MainStudio mainStudio;
        #region 初始化

        public ServerConnecter()
        {
            InitializeComponent();
        }

        public ServerConnecter(MainStudio _mainStudio)
        {
            InitializeComponent();
            mainStudio = _mainStudio;
        }
        

        #endregion


        #region 数据更新事件
            //选择一个服务器
            private void cbServer_DropDown(object sender, EventArgs e)
            {
                try
                {
                    if (cbServer.DataSource == null)
                    {
                        cbServer.DataSource = ScBO.GetSqlServerOverLan();
                    }
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }
           
            //刷新
            private void btnFlush_Click(object sender, EventArgs e)
            {
                try
                {
                    cbServer.DataSource = ScBO.GetSqlServerOverLan();
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }

            //选择一个数据库
            private void cbDatabase_DropDown(object sender, EventArgs e)
            {
                try
                {
                    if (cbDatabase.DataSource == null)
                    {
                        cbDatabase.DataSource = ScBO.GetDatabaseNameFromServer
                           (
                              cbServer.Text.Trim(),
                              String.IsNullOrEmpty(txtUsername.Text.Trim())?"":txtUsername.Text.Trim(),
                              String.IsNullOrEmpty(txtPassword.Text.Trim())?"":txtPassword.Text.Trim(),
                              GetVerifyMode()
                           );
                    }
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }

        #endregion
        
       
        #region 状态改变事件
            //选择一个服务器
            private void cbServer_TextChanged(object sender, EventArgs e)
            {
               SetControlState(cbServer.Name);
            }
      
            //SQL Server验证
            private void rbSqlserver_CheckedChanged(object sender, EventArgs e)
             {
               SetControlState(rbSqlserver.Name);
             }

            //Windows验证
            private void rbWindows_CheckedChanged(object sender, EventArgs e)
            {
               SetControlState(rbWindows.Name);
            }
        
            //用户名
            private void txtUsername_TextChanged(object sender, EventArgs e)
            {
               SetControlState(txtUsername.Name);
            }

            //密码
            private void txtPassword_TextChanged(object sender, EventArgs e)
            {
                SetControlState(txtPassword.Name);
            }

        #endregion
        

        #region 私有方法

        /// <summary>
        /// 得到验证模式
        /// </summary>
        /// <returns></returns>
		 private enumVerifyMode GetVerifyMode()
            {
                return rbWindows.Checked ? enumVerifyMode.Windows : enumVerifyMode.SQLServer;
            }

         private void SetControlState(string ContorlName)
         {
             switch (ContorlName)
             {
                 case "cbServer":
                     groupBox2.Enabled = String.IsNullOrEmpty(cbServer.Text.Trim()) ? false : true;
                     CleanControlInfo();
                     break;

                 case "rbSqlserver":
                     if (rbSqlserver.Checked)
                     {
                         panel1.Enabled = true;
                         groupBox2.Enabled = (String.IsNullOrEmpty(txtUsername.Text.Trim()) ? false : true) && (String.IsNullOrEmpty(cbServer.Text.Trim()) ? false : true);
                     }
                     else
                     {
                         groupBox2.Enabled = String.IsNullOrEmpty(cbServer.Text.Trim()) ? false : true;
                         panel1.Enabled = false;
                     }
                     CleanControlInfo();
                     break;

                 case "rbWindows":
                     if (rbWindows.Checked)
                     {
                         panel1.Enabled = false;
                         groupBox2.Enabled = String.IsNullOrEmpty(cbServer.Text.Trim()) ? false : true;

                     }
                     else
                     {
                         panel1.Enabled = true;
                         groupBox2.Enabled = (String.IsNullOrEmpty(txtUsername.Text.Trim()) ? false : true) && (String.IsNullOrEmpty(cbServer.Text.Trim()) ? false : true);
                     }
                     CleanControlInfo();
                     break;

                 case "txtUsername":
                     groupBox2.Enabled = String.IsNullOrEmpty(txtUsername.Text.Trim()) ? false : true;
                     CleanControlInfo();
                     break;

                 case "txtPassword":
                     CleanControlInfo();
                     break;
             }
         }

         private void CleanControlInfo()
         {
             //txtUsername.Clear();
             //if (String.IsNullOrEmpty(cbServer.Text.Trim()))
             //{
             //    cbDatabase.Text = "";
             cbDatabase.DataSource = null;
             //}
         }
	   
        #endregion


        #region 按钮事件

          private void btnSelCommit_Click(object sender, EventArgs e)
          {
              try
              {
                  List<string> list = ScBO.GetTablesFromDataBase(cbServer.Text.Trim(), cbDatabase.Text.Trim(), txtUsername.Text.Trim(), txtPassword.Text.Trim(), GetVerifyMode());
                  string conStr = ScBO.GetConnectString(cbServer.Text.Trim(), cbDatabase.Text.Trim(), txtUsername.Text.Trim(), txtPassword.Text.Trim(), GetVerifyMode());
                  using (TableSelector ts = new TableSelector(conStr, list,mainStudio) { Owner = this })
                  {
                      ts.ShowDialog();
                  }
              }
              catch(Exception ex)
              {
                  //throw ex;
                  MessageBox.Show("连接出错");
              }
          }

          private void btnQueCommit_Click(object sender, EventArgs e)
          {
              try
              {
                  string conStr = txtConnectStr.Text.Trim();
                  List<string> list = ScBO.GetTablesFromDataBase(conStr);
                  using (TableSelector ts = new TableSelector(conStr, list,mainStudio) { Owner = this })
                  {
                      ts.ShowDialog();
                  }
              }
              catch(Exception ex)
              {
                  throw ex;
              }
          }

          private void btnCancel_Click(object sender, EventArgs e)
          {
              this.Close();
          }

          private void btnTestStr_Click(object sender, EventArgs e)
          {
              string conStr = txtConnectStr.Text.Trim();
              if (!String.IsNullOrEmpty(conStr))
              {
                  List<string> list = ScBO.GetTablesFromDataBase(conStr);
                  if (list.Count > 0)
                  {
                      MessageBox.Show("成功！");
                  }
                  else
                  {
                      MessageBox.Show("连接出错或者表中无数据");
                  }
              }
              else
              {
                  MessageBox.Show("连接出错或者表中无数据");
              }

          }

         #endregion

          private void btnTestCon_Click(object sender, EventArgs e)
          {
              List<string> list = ScBO.GetTablesFromDataBase(cbServer.Text.Trim(), cbDatabase.Text.Trim(), txtUsername.Text.Trim(), txtPassword.Text.Trim(), GetVerifyMode());
              string conStr = ScBO.GetConnectString(cbServer.Text.Trim(), cbDatabase.Text.Trim(), txtUsername.Text.Trim(), txtPassword.Text.Trim(), GetVerifyMode());
              if (!String.IsNullOrEmpty(conStr))
              {
                  MessageBox.Show("成功！");
              }
              else
              {
                  MessageBox.Show("连接出错或者表中无数据");
              }
          }

       

        

       
    


        

        

        

        //private void Set
    }

        
}
